package cn.vc.content.controller.video;

import cn.vc.auth.entity.po.VUser;
import cn.vc.basicmodule.entity.Result;
import cn.vc.content.entity.video.VideoReview;
import cn.vc.content.service.video.VideoReviewService;
import cn.vc.content.service.video.VideoService;
import cn.vc.content.util.SecurityUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评论及回复
 */
@RestController
@RequestMapping("/videoreview")
public class VideoReviewController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private VideoReviewService videoReviewService;
    @Resource
    private VideoService videoService;


    /**
     * 评论
     * @param videoId
     * @param content
     * @return
     */
    @PostMapping("/1")
//    @PreAuthorize("hasAnyAuthority('ordinary')")
    public Result<VideoReview> review(@RequestParam("videoId") Long videoId, @RequestParam("content") String content) {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();

        VideoReview review = new VideoReview();
        review.setVideoIds(videoId);
        review.setUserId(myUser.getId());
        review.setUsername(myUser.getUsername());
        review.setVideoId(videoId);
        review.setContent(content);
        boolean bo = videoReviewService.review(review);
        if (bo) {
            // 评论数加一
            videoService.review(videoId);
            return new Result<>(1, review, null);
        } else {
            return new Result<>(0);
        }

    }
    /**
     * 回复
     * @param videoIds
     * @param reviewId
     * @param content
     * @return
     */
    @PostMapping("/2")
//    @PreAuthorize("hasAnyAuthority('ordinary')")
    public Result<VideoReview> recover(@RequestParam("videoIds") Long videoIds, @RequestParam("reviewId") Long reviewId, @RequestParam("content") String content) {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();

        VideoReview recover = new VideoReview();
        recover.setVideoIds(videoIds);
        recover.setUserId(myUser.getId());
        recover.setUsername(myUser.getUsername());
        recover.setReviewId(reviewId);
        recover.setContent(content);
        boolean bo = videoReviewService.review(recover);
        if (bo) {
            // 评论数加一
            videoService.review(videoIds);
            return new Result<>(1, recover, null);
        } else {
            return new Result<>(0);
        }
    }


    /**
     * 查询视频评论
     * @param videoId
     * @return
     */
    @GetMapping("/1/{videoId}")
    public Result<List<VideoReview>> selectReview(@PathVariable("videoId") Long videoId) {
        List<VideoReview> videoReviewList = videoReviewService.selectReview(videoId);
        return new Result<>(1, videoReviewList);
    }
}
